package com.kingmed.kmss.bpmn.modules.backup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingmed.kmss.bpmn.modules.backup.mapper.BizApprovalTrailMapper;
import com.kingmed.kmss.bpmn.modules.backup.model.BizApprovalStatus;
import com.kingmed.kmss.bpmn.modules.backup.model.BizApprovalTrail;
import com.kingmed.kmss.bpmn.modules.backup.service.BizApprovalTrailService;
import com.kingmed.kmss.common.constant.bpmn.BpmnConstant;
import com.kingmed.kmss.common.dto.bpmn.BizApprovalTrailDTO;
import com.kingmed.kmss.common.qc.bpmn.ApprovalTrailQC;
import com.kingmed.kmss.common.utils.BeanCopyUtils;
import com.kingmed.kmss.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 审批追踪表 服务实现类
 * </p>
 *
 * @author wzg
 * @since 2023-02-19
 */
@Service
public class BizApprovalTrailServiceImpl extends ServiceImpl<BizApprovalTrailMapper, BizApprovalTrail> implements BizApprovalTrailService {

	@Override
	public List<BizApprovalTrailDTO> queryApprovalTrail(ApprovalTrailQC qc) {
		
		QueryWrapper<BizApprovalTrail> wrapper = new QueryWrapper<BizApprovalTrail>();
		LambdaQueryWrapper<BizApprovalTrail> lambda = wrapper.lambda();
		if (StringUtils.isNotEmpty(qc.getBusinessType())) {
			lambda.eq(BizApprovalTrail::getBizType, qc.getBusinessType());
		}
		
		if (StringUtils.isNotEmpty(qc.getBusinessId())) {
			lambda.eq(BizApprovalTrail::getBusinessId, qc.getBusinessId());
		}

		if (StringUtils.isNotEmpty(qc.getBusinessKey())) {
			lambda.eq(BizApprovalTrail::getBusinessId, qc.getBusinessKey());
		}
		
		lambda.orderByDesc(BizApprovalTrail::getApproveTime);
		
		return BeanCopyUtils.copyList(list(wrapper), BizApprovalTrailDTO.class);
	}

	@Override
	public void saveApprovalTrail(BizApprovalTrail trail) {
		save(trail);
		
	}

	@Override
	public String getProcInstIdByBiz(String bizType, String bizId) {
		List<BizApprovalTrail> trails = getBaseMapper().getProcInstIdByBiz(bizType, bizId);
		
		if (trails != null && trails.size() > 0) {
			if (trails.size() > 1) {
				log.warn("启动了多个审批流程? " + bizType + ", " + bizId);				
			}
			
			return trails.get(0).getProcInstId();
		}
		
		return null;
	}

	@Override
	public List<BizApprovalTrail> getMyCompleteProcesses(String bizType, String userId) {
		List<BizApprovalTrail> trails = getBaseMapper().getMyApprovedBusiness(bizType, userId, BpmnConstant.PROCESS_REQUEST_DESC);
		
		return trails;
	}

	@Override
	public Page<BizApprovalStatus> getMySubmitProcesses(String bizType, String userId, String currApprover,
														String auditStatus, int pageNum, int pageSize, String startTime, String endTime, String requestByName) {

			Page<BizApprovalStatus> page = new Page<>(pageNum, pageSize);
			page = getBaseMapper().getMySubmitted(userId, bizType, currApprover, auditStatus,startTime,endTime, page,requestByName);
			return page;
	}

	@Override
	public List<BizApprovalTrail> listByProcessInstanceId(String processInstanceId) {
		return super.list(new LambdaQueryWrapper<BizApprovalTrail>().eq(BizApprovalTrail::getProcInstId,processInstanceId).orderByAsc(BizApprovalTrail::getId));
	}

}
